package lx.utils.demo;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.RelativeLayout;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;
import com.lx.utils.R;

public final class ActivityImageRequestDemo extends Activity
        implements OnClickListener {

    private static final String[] URLS = {
            "http://img.my.csdn.net/uploads/201403/03/1393854094_4652.jpg",
            "http://img.my.csdn.net/uploads/201403/03/1393854084_6138.jpg",
            "http://img.my.csdn.net/uploads/201403/03/1393854084_1323.jpg",
            "http://img.my.csdn.net/uploads/201403/03/1393854084_8439.jpg",
            "http://img.my.csdn.net/uploads/201403/03/1393854083_6511.jpg",
            "http://img.my.csdn.net/uploads/201403/03/1393854083_2323.jpg"
    };

    private GridView mGridView;
    private Button mBtnBegin;

    private ImageAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_request_demo);
        mGridView = (GridView) findViewById(R.id.aird_grid_view);
        mBtnBegin = (Button) findViewById(R.id.aird_btn_begin);
        mBtnBegin.setOnClickListener(this);
        mAdapter = new ImageAdapter(this);
        mGridView.setAdapter(mAdapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.aird_btn_begin:
            loadImage("http://img.my.csdn.net/uploads/201403/03/1393854084_6138.jpg");
            break;
        }
    }

    private void loadImage(String imgUrl) {

        RequestQueue mQueue = Volley.newRequestQueue(this);
        ImageRequest imgRequest = new ImageRequest(imgUrl,
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap bitmap) {
                        addImage(bitmap);
                    }
                },
                300,
                200,
                Config.ARGB_8888,
                new ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError arg0) {

                    }
                });
        mQueue.add(imgRequest);
    }

    private void addImage(Bitmap bitmap) {
        mAdapter.addBitmap(bitmap);
        mAdapter.notifyDataSetChanged();
    }

    private static class ImageAdapter extends BaseAdapter {

        private Context mContext;
        private ArrayList<Bitmap> mBitmapList;

        public ImageAdapter(Context context) {
            mContext = context;
            mBitmapList = new ArrayList<Bitmap>();
        }

        @Override
        public int getCount() {
            return mBitmapList == null ? 0 : mBitmapList.size();
        }

        @Override
        public Object getItem(int position) {
            return mBitmapList == null ? null : mBitmapList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = new ImageHolder(mContext);
            }
            ImageHolder holder = (ImageHolder) convertView;
            holder.mImage.setImageBitmap(mBitmapList.get(position));
            return convertView;
        }

        public void addBitmap(Bitmap bitmap) {
            mBitmapList.add(bitmap);
        }
    }

    private static class ImageHolder extends RelativeLayout {

        public ImageView mImage;

        public ImageHolder(Context context) {
            super(context);
            this.setBackgroundColor(Color.BLACK);
            addImageView();
            addProgressBar();
        }

        private void addImageView() {
            mImage = new ImageView(getContext());
            mImage.setScaleType(ScaleType.FIT_CENTER);
            RelativeLayout.LayoutParams params =
                    new RelativeLayout.LayoutParams(
                            LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            params.addRule(RelativeLayout.CENTER_IN_PARENT);
            this.addViewInLayout(mImage, -1, params);
        }

        // TODO: add progress bar
        private void addProgressBar() {
        }
    }
}
